<?php

function summary_grievances($periode_id){
	
	$header = array(
		'No'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Uraian'=>array('style'=>'border-bottom:2px solid;width:70%'),  
		'Sangat Tdk. Setuju'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Tdk. Setuju'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Tidak Tahu'=>array('style'=>'text-align:right;border-bottom:2px solid;width:5%'),  
		'Setuju'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Sangat Setuju'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
	);
	
	
	$query =  "SELECT * FROM grievance_kelompok where grievance_periode_id={$periode_id}";
	$result = my_query($query);
	$row = array();
	$kelompok_no=0 ;
	while($ey = my_fetch_array($result) ){
		$kelompok_no++ ;
		$row[] = array(
			'no'=>$kelompok_no, 
			'uraian'=>  $ey['nama_grievance_kelompok']  , 
			'sts'=>  ' ' ,   
			'ts'=> ' ' ,    
			'tt'=> ' ' ,   
			's'=> ' ' ,   
			'ss'=> ' ' ,    
		);

		$query =  "SELECT * FROM grievance_sub 
			WHERE grievance_kelompok_id=".$ey['grievance_kelompok_id'] ;
		$result1 = my_query($query);		
		$sub_no=64 ;
		while($ey1 = my_fetch_array($result1) ){
			$sub_no++ ;
			if(empty($ey1['nama_grievance_sub'])){
			}else{
				$row[] = array(
					'no'=>chr($sub_no), 
					'uraian'=>  $ey1['nama_grievance_sub']  , 
					'sts'=>  ' ' ,   
					'ts'=> ' ' ,    
					'tt'=> ' ' ,   
					's'=> ' ' ,   
					'ss'=> ' ' ,    
				);
			}
			$query =  "SELECT * FROM grievance 
				WHERE grievance_sub_id=".$ey1['grievance_sub_id'] ;
			$result2 = my_query($query);		
			while($ey2 = my_fetch_array($result2) ){
				$query =  "SELECT jawaban,COUNT(*) as total FROM grievance_entry_detail
					WHERE grievance_id=".$ey2['grievance_id']." 
					GROUP BY jawaban" ;
				$result3 = my_query($query);		
				$sts=$ts=$tt=$s=$ss=0 ; 				
				while($ey3 = my_fetch_array($result3) ){
					if($ey3['jawaban']=='Sangat Tdk. Setuju'){
						$sts=$ey3['total'] ;
					}elseif($ey3['jawaban']=='Tdk. Setuju'){
						$ts=$ey3['total'] ;
					}elseif($ey3['jawaban']=='Tidak Tahu'){
						$tt=$ey3['total'] ;
					}elseif($ey3['jawaban']=='Setuju'){
						$s=$ey3['total'] ;
					}elseif($ey3['jawaban']=='Sangat Setuju'){
						$ss=$ey3['total'] ;
					}
				}	
				$row[] = array(
					'no'=>'-', 
					'uraian'=> $ey2['nama_grievance']  , 
					'sts'=> position_text_align ($sts ,  'right'),  
					'ts'=> position_text_align ($ts ,   'right'),  
					'tt'=> position_text_align ($tt ,  'right'), 
					's'=> position_text_align ($s ,   'right'),
					'ss'=> position_text_align ($ss , 'right'),     
				);
			}
		}	
	}	
	$datas = table_rows($row);
	my_set_code_js(' 
		function postDataPeriode(periode_id){ 
			location.href=\'index.php?com='.$_GET['com'].'&periode_id=\'+periode_id ;
		}
	');
	
	$query_check_periode = " SELECT * FROM grievance_periode ORDER BY grievance_periode_id DESC";
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['grievance_periode_id']] = date('m-Y',strtotime($prow['periode']));
	}
	$fperiode_id = array(
		'id'=>'periode_id',
		'value'=>$periode_id ,
		'name'=>'periode_id',
		'onchange'=>'javascript:postDataPeriode(this.value)'
	);
	$form_dpwerio = form_dropdown($fperiode_id,$prs);
	$periodes = my_get_data_by_id('grievance_periode','grievance_periode_id',$periode_id );
	$view  = form_header( "..." , "..."  );
	$view .= form_field_display( '<span class="label_form"> '.$form_dpwerio.'</span>'   , "<b>Periode</b>"    ); 
	$menuontop = array(  
		'Excel' => array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=export_excel&periode_id='.$periode_id.'\''),
		'Cetak' =>array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=cetak&periode_id='.$periode_id.'&ip='.rand(0,10000).'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,scrollbars,resizable=0\');'),
		);
	$printmenu = additional_menu_on_list( $menuontop , '');
	return  $view.$printmenu.table_builder($header , $datas ,  6 , false , $paging  );
}


function export_excel($periode_id){
	$header = array(
		'No'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Uraian'=>array('style'=>'border-bottom:2px solid;width:70%'),  
		'Sangat Tdk. Setuju'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Tdk. Setuju'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Tidak Tahu'=>array('style'=>'text-align:right;border-bottom:2px solid;width:5%'),  
		'Setuju'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Sangat Setuju'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
	);
	
	
	$query =  "SELECT * FROM grievance_kelompok where grievance_periode_id={$periode_id}";
	$result = my_query($query);
	$row = array();
	$kelompok_no=0 ;
	while($ey = my_fetch_array($result) ){
		$kelompok_no++ ;
		$row[] = array(
			'no'=>$kelompok_no, 
			'uraian'=>  $ey['nama_grievance_kelompok']  , 
			'sts'=>  ' ' ,   
			'ts'=> ' ' ,    
			'tt'=> ' ' ,   
			's'=> ' ' ,   
			'ss'=> ' ' ,    
		);

		$query =  "SELECT * FROM grievance_sub 
			WHERE grievance_kelompok_id=".$ey['grievance_kelompok_id'] ;
		$result1 = my_query($query);		
		$sub_no=64 ;
		while($ey1 = my_fetch_array($result1) ){
			$sub_no++ ;
			if(empty($ey1['nama_grievance_sub'])){
			}else{
				$row[] = array(
					'no'=>chr($sub_no), 
					'uraian'=>  $ey1['nama_grievance_sub']  , 
					'sts'=>  ' ' ,   
					'ts'=> ' ' ,    
					'tt'=> ' ' ,   
					's'=> ' ' ,   
					'ss'=> ' ' ,    
				);
			}
			$query =  "SELECT * FROM grievance 
				WHERE grievance_sub_id=".$ey1['grievance_sub_id'] ;
			$result2 = my_query($query);		
			while($ey2 = my_fetch_array($result2) ){
				$query =  "SELECT jawaban,COUNT(*) as total FROM grievance_entry_detail
					WHERE grievance_id=".$ey2['grievance_id']." 
					GROUP BY jawaban" ;
				$result3 = my_query($query);		
				$sts=$ts=$tt=$s=$ss=0 ; 				
				while($ey3 = my_fetch_array($result3) ){
					if($ey3['jawaban']=='Sangat Tdk. Setuju'){
						$sts=$ey3['total'] ;
					}elseif($ey3['jawaban']=='Tdk. Setuju'){
						$ts=$ey3['total'] ;
					}elseif($ey3['jawaban']=='Tidak Tahu'){
						$tt=$ey3['total'] ;
					}elseif($ey3['jawaban']=='Setuju'){
						$s=$ey3['total'] ;
					}elseif($ey3['jawaban']=='Sangat Setuju'){
						$ss=$ey3['total'] ;
					}
				}	
				$row[] = array(
					'no'=>'-', 
					'uraian'=> $ey2['nama_grievance']  , 
					'sts'=> position_text_align ($sts ,  'right'),  
					'ts'=> position_text_align ($ts ,   'right'),  
					'tt'=> position_text_align ($tt ,  'right'), 
					's'=> position_text_align ($s ,   'right'),
					'ss'=> position_text_align ($ss , 'right'),     
				);
			}
		}	
	}	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas , 6 ,false ); 
}


function print_plain($periode_id){

	$datas = array();  
	$datas[] = array(
		 'k1'=>array("No." ,5 ,STR_PAD_LEFT),
		 'k2'=>array("Uraian" ,55,STR_PAD_RIGHT),
		 'k3'=>array("Sangat" ,8,STR_PAD_BOTH),
		 'k4'=>array("Tdk." ,8,STR_PAD_BOTH),  
		 'k6'=>array("Tidak" ,8,STR_PAD_BOTH),  
		 'k7'=>array("Setuju" ,8,STR_PAD_BOTH),  
		 'k8'=>array("Sangat" ,8,STR_PAD_BOTH),  
	); 
	$datas[] = array(
		 'k1'=>array(" " ,5 ,STR_PAD_LEFT),
		 'k2'=>array(" " ,55,STR_PAD_RIGHT),
		 'k3'=>array("Tdk." ,8,STR_PAD_BOTH),
		 'k4'=>array("Setuju" ,8,STR_PAD_BOTH),  
		 'k6'=>array("Tahu" ,8,STR_PAD_BOTH),  
		 'k7'=>array(" " ,8,STR_PAD_BOTH),  
		 'k8'=>array("Setuju" ,8,STR_PAD_BOTH),  
	); 
	$datas[] = array(
		 'k1'=>array(" " ,5 ,STR_PAD_LEFT),
		 'k2'=>array(" " ,55,STR_PAD_RIGHT),
		 'k3'=>array("Setuju" ,8,STR_PAD_BOTH),
		 'k4'=>array(" " ,8,STR_PAD_BOTH),  
		 'k6'=>array(" " ,8,STR_PAD_BOTH),  
		 'k7'=>array(" " ,8,STR_PAD_BOTH),  
		 'k8'=>array(" " ,8,STR_PAD_BOTH),  
	); 	
	$i=0;
	
	$query =  "SELECT * FROM grievance_kelompok where grievance_periode_id={$periode_id}";
	$result = my_query($query);
	$row = array();
	$kelompok_no=0 ;
	while($ey = my_fetch_array($result) ){
		$kelompok_no++ ;
		$datas[] = array(
			 'k1'=>array($kelompok_no ,5 ,STR_PAD_LEFT),
			 'k2'=>array(substr( $ey['nama_grievance_kelompok'],0,55) ,55,STR_PAD_RIGHT),
			 'k3'=>array(" " ,8,STR_PAD_BOTH),
			 'k4'=>array(" " ,8,STR_PAD_BOTH),  
			 'k6'=>array(" " ,8,STR_PAD_BOTH),  
			 'k7'=>array(" " ,8,STR_PAD_BOTH),  
			 'k8'=>array(" " ,8,STR_PAD_BOTH),  
		); 			
		$query =  "SELECT * FROM grievance_sub 
			WHERE grievance_kelompok_id=".$ey['grievance_kelompok_id'] ;
		$result1 = my_query($query);		
		$sub_no=64 ;
		while($ey1 = my_fetch_array($result1) ){
			$sub_no++ ;
			if(empty($ey1['nama_grievance_sub'])){
			}else{
				$datas[] = array(
					 'k1'=>array(chr($sub_no) ,5 ,STR_PAD_LEFT),
					 'k2'=>array(substr(  $ey1['nama_grievance_sub'],0,55) ,55,STR_PAD_RIGHT),
					 'k3'=>array(" " ,8,STR_PAD_BOTH),
					 'k4'=>array(" " ,8,STR_PAD_BOTH),  
					 'k6'=>array(" " ,8,STR_PAD_BOTH),  
					 'k7'=>array(" " ,8,STR_PAD_BOTH),  
					 'k8'=>array(" " ,8,STR_PAD_BOTH),  
				); 						
			}
			$query =  "SELECT * FROM grievance 
				WHERE grievance_sub_id=".$ey1['grievance_sub_id'] ;
			$result2 = my_query($query);		
			while($ey2 = my_fetch_array($result2) ){
				$query =  "SELECT jawaban,COUNT(*) as total FROM grievance_entry_detail
					WHERE grievance_id=".$ey2['grievance_id']." 
					GROUP BY jawaban" ;
				$result3 = my_query($query);		
				$sts=$ts=$tt=$s=$ss=0 ; 				
				while($ey3 = my_fetch_array($result3) ){
					if($ey3['jawaban']=='Sangat Tdk. Setuju'){
						$sts=$ey3['total'] ;
					}elseif($ey3['jawaban']=='Tdk. Setuju'){
						$ts=$ey3['total'] ;
					}elseif($ey3['jawaban']=='Tidak Tahu'){
						$tt=$ey3['total'] ;
					}elseif($ey3['jawaban']=='Setuju'){
						$s=$ey3['total'] ;
					}elseif($ey3['jawaban']=='Sangat Setuju'){
						$ss=$ey3['total'] ;
					}
				}
				$datas[] = array(
					 'k1'=>array('-' ,5 ,STR_PAD_LEFT),
					 'k2'=>array(substr(  $ey2['nama_grievance'],0,55) ,55,STR_PAD_RIGHT),
					 'k3'=>array($sts ,8,STR_PAD_BOTH),
					 'k4'=>array($ts,8,STR_PAD_BOTH),  
					 'k6'=>array($tt ,8,STR_PAD_BOTH),  
					 'k7'=>array($s  ,8,STR_PAD_BOTH),  
					 'k8'=>array($ss ,8,STR_PAD_BOTH),  
				); 
			}
		}	
	}	
	$view = 	str_pad("REPORT GRIEVANCES",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);
}